package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.*;
import com.ws.ssmzongjie3.service.*;
import com.ws.ssmzongjie3.util.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("/route")
@Controller
public class RouteController {
    @Autowired
    private RouteService routeService;
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private Route_MapService route_mapService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private ColumnService columnService;
    @RequestMapping("/list")
    public String select(Model model, ProVo proVo, Route route){
        System.out.println("links:"+route);

        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
//        List<Route> routes = routeService.listRoute(route);
        List<Route> route1 = routeService.listRouteWithBusinessAndColumn(route);
        PageInfo pageInfo = new PageInfo(route1);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("route",route);
        return "/WEB-INF/admin/route/list";
    }
    @RequestMapping("/add")
    public String add(Model model, Route route) {

        List<Column> columns = columnService.listColumn(new Column());
        List<Business> businesses = businessService.listBusiness(new Business());
        model.addAttribute("businesses",businesses);
        model.addAttribute("columns",columns);
        return "/WEB-INF/admin/route/add";
    }
    @RequestMapping("/insert")
    public String insert(Route route, MultipartFile image,MultipartFile image2, MultipartFile[] image3) throws IOException {

        if ((image.getSize() != 0)||!(image.getOriginalFilename().equals(""))){
            System.out.println("文本框名字"+image.getName());
            System.out.println("文件名字"+image.getOriginalFilename());
            System.out.println("文件大小"+image.getSize());
            System.out.println("文件格式"+image.getContentType());

            String uuid = UUID.randomUUID().toString().replace("-", "");
            System.out.println(uuid);
            String fileName = uuid+"."+image.getOriginalFilename().split("\\.")[1];
            System.out.println(fileName);
            image.transferTo(new File(Res.fileDir+fileName));
            route.setRoute_photo(fileName);
        }
        routeService.insertRoute(route);
        Carousel carousel = new Carousel(route.getRoute_name(), "", 1, route.getRoute_id());
        Route_Map routeMap = new Route_Map(route.getRoute_id(), "", 1);
        if ((image2.getSize() != 0)||!(image2.getOriginalFilename().equals(""))){

            String uuid = UUID.randomUUID().toString().replace("-", "");
            String fileName = uuid+"."+image.getOriginalFilename().split("\\.")[1];
            System.out.println(fileName);
            image2.transferTo(new File(Res.fileDir+fileName));
            carousel.setPicture_address(fileName);

            carouselService.insertCarousel(carousel);
        }
        for (MultipartFile img : image3){
            if ((img.getSize() != 0)||!(img.getOriginalFilename().equals(""))){

                String fileName = UUID.randomUUID().toString().replace("-", "")+"."+img.getOriginalFilename().split("\\.")[1];
                System.out.println(fileName);
                img.transferTo(new File(Res.fileDir+fileName));
                routeMap.setRoute_img_url(fileName);
                route_mapService.insertRoute_Map(routeMap);

            }
        }





        return "redirect:/route/list";
    }
    @RequestMapping("/update")
    public String update(Route route, MultipartFile image) throws IOException {
        // 获取旧路线信息
        Route oldRoute = routeService.getRouteById(route);
        // 如果上传了新图片
        if ((image.getSize() != 0)||!(image.getOriginalFilename().equals(""))){
            // 删除旧图片
            if (oldRoute != null && oldRoute.getRoute_photo() != null && !oldRoute.getRoute_photo().isEmpty()) {
                File oldPhotoFile = new File(Res.fileDir + oldRoute.getRoute_photo());
                if (oldPhotoFile.exists()) {
                    oldPhotoFile.delete();
                }
            }

            // 保存新图片
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String fileName = uuid+"."+image.getOriginalFilename().split("\\.")[1];
            image.transferTo(new File(Res.fileDir+fileName));
            route.setRoute_photo(fileName);
        }
        routeService.updateRoute(route);
        return "redirect:/route/list";
    }
    @RequestMapping("/delete")

    public String delete(Route route) {
        routeService.updateRouteDelete(route);
        return "redirect:/route/list";
    }
//    public String delete(Route route) {
//        // 获取路线信息
//        Route oldRoute = routeService.getRouteById(route);
//        // 删除路线
//        routeService.deleteRoute(route);
//        // 如果路线有图片，删除对应的图片文件
//        if (oldRoute != null && oldRoute.getRoute_photo() != null && !oldRoute.getRoute_photo().isEmpty()) {
//            File photoFile = new File(Res.fileDir + oldRoute.getRoute_photo());
//            if (photoFile.exists()) {
//                photoFile.delete();
//            }
//        }
//        return "redirect:/route/list";
//    }
    @RequestMapping("/huixian")
    public String huixian(Route route, Model model) {
        List<Route> route2 = routeService.listRouteWithBusinessAndColumn(route);

        List<Column> columns = columnService.listColumn(new Column());
        List<Business> businesses = businessService.listBusiness(new Business());
        model.addAttribute("businesses",businesses);
        model.addAttribute("columns",columns);
        Route route1 = routeService.getRouteById(route);
        for (Route route3 : route2) {
            route1 = route3;
        }

        model.addAttribute("route", route1);
        return "/WEB-INF/admin/route/update";
    }


}
